<?php
namespace {dbName}\Modules\Frontend\Controllers;
 
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use {dbName}\Model;


class {tblName}Controller extends ControllerBase
{
    public function indexAction()
    {
        //查询条件
        if($this->request->isPost())
		{
			$str_where="1=1";
            $data["post"] = $this->request->getPost();
			/*
  [loop start]
            if(!empty($data["post"]["[fldName]"])){
                $str_where.=" and [fldName]='".$data["post"]["[fldName]"]."'";
            }
  [loop end]
			*/
			$cond["conditions"]=$str_where;

			//统计总数用于分页
			$reoordcount =Model\{tblName}::find($cond)->count();
			$data["post"]["reoordcount"]=$reoordcount;

			//当前页记录
			$offset = ($data["post"]["pageindex"]-1) * $data["post"]["pagesize"];
			$cond["limit"]= array("number" => $data["post"]["pagesize"], "offset" => $offset);
			$cond["order"]="id desc";
			$data['list'] =Model\{tblName}::find($cond );
			$this->success("ok",$data["list"]->toArray());
        }

        //传值给view
        $this->view->data = $data;
    }

    //保存
    public function addAction()
    {
        if($this->request->isPost()){
            $model = new Model\{tblName}();
            $post = $this->request->getPost();
            $model->assign($post);

            if($model->save() === false){
                $message = $model->getMessages();
                $this->error($message);
            }else{
                $this->success("ok");
            }
          }
    }

    //获取
    public function getAction($id=0)
    {
        $model = new Model\{tblName}();
        if($id>0){
            $model=$model::findFirst("id=".$id);
            if(!$model)$this->error("查询不到对应的数据",10001);
        }else{
            //设置添加默认值
        }
        $this->success("ok",$model->toArray());
    }
    //删除
   public  function  deleteAction($id=0)
   {
        if($id>0){
            $model = new Model\{tblName}();
            $da=$model::findFirst("id=".$id);
            //print_r($da->ToArray());
            $da->delete();
            $this->success();
        }
    } 
}

